<?php
/**
 * Created by PhpStorm.
 * User: xiaoxin
 * Date: 2018/10/29 0029
 * Time: 17:30
 */

namespace app\index\controller;
use app\index\model\Question;
use app\index\model\Reply;
use app\index\model\User;
use think\Controller;
use think\Db;

class HomePage extends Controller
{
	/**
	 * 我的主页页面
	 * @return mixed
	 * @throws \think\exception\DbException
	 */
	public function index()
	{
		/**
		 * 用户中心，如果是从外面带参进来的走带参的ID  其他方式则走session里面的id
		 */
		if (input('user_id')) {
			$user = User::get(input('user_id'));
			$this->assign('user', $user);
			$this->assign('title', $user->nickname . '的主页');
			$ques = Question::where('user_id', input('user_id'))->select();
			$this->assign('ques', $ques);
			$reply = Db::name('reply')
				->alias('t1')
				->field('t1.create_time,t1.content,t2.title,t3.nickname,t2.question_id')
				->join('question t2', 't1.question_id = t2.question_id')
				->join('user t3', 't1.user_id = t3.user_id')
				->where('t1.user_id', input('user_id'))
				->select();
			$this->assign('reply', $reply);
		} else {
			$user = User::get(session('user_id'));
			$ques = Question::where('user_id', session('user_id'))->select();
			$this->assign('ques', $ques);

			$reply = Db::name('reply')
				->alias('t1')
				->field('t1.create_time,t1.content,t2.title,t3.nickname,t2.question_id')
				->join('question t2', 't1.question_id = t2.question_id')
				->join('user t3', 't1.user_id = t3.user_id')
				->where('t1.user_id', session('user_id'))
				->select();
			$this->assign('title', $user->nickname . '的主页');
			$this->assign('reply', $reply);
			$this->assign('user', $user);
		}
		return $this->fetch();
	}

}